package com.fh.modules.ems.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.query.QueryWrapperUtil;
import com.fh.modules.ems.entity.EmsCoupon;
import com.fh.modules.ems.service.IEmsCouponService;
import com.fhMall.request.AddMemberCouponRequest;
import com.fhMall.request.DeleteMemberCouponRequest;
import com.fhMall.request.QueryMemberCouponRequest;
import com.fhMall.request.SaveMemberCouponRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Title: Controller
 * @Description: 优惠券
 * @author： qiujunda
 * @date： 2019-10-26
 * @version： V1.0
 */
@RestController
@Slf4j
@Api("优惠券")
public class EmsCouponController {
    @Autowired
    private IEmsCouponService emsCouponService;

    /**
     * 分页列表查询
     *
     * @param emsCoupon
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/ems/emsCoupon/list")
    @ApiOperation("分页列表查询")
    @AutoLog(value = "优惠券分页查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryPageList(EmsCoupon emsCoupon,
                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                HttpServletRequest req) {
        QueryWrapper<EmsCoupon> queryWrapper = new QueryWrapper<EmsCoupon>();
        emsCoupon.setDelFlag(CommonConstant.DEL_FLAG_0);
        QueryWrapperUtil.initQueryWrapper(queryWrapper, emsCoupon);
        Page<EmsCoupon> page = new Page<EmsCoupon>(pageNo, pageSize);
        IPage<EmsCoupon> pageList = emsCouponService.page(page, queryWrapper);
        return Result.ok(pageList);
    }


    @PostMapping(value = "/ems/emsCoupon/add")
    @ApiOperation("添加")
    @AutoLog("添加优惠券")
    public Result add(@RequestBody EmsCoupon emsCoupon) {
        emsCoupon.setDelFlag(CommonConstant.DEL_FLAG_0);
        emsCouponService.save(emsCoupon);
        return Result.ok("添加成功");

    }


    @DeleteMapping(value = "/ems/emsCoupon/deleteBatch")
    @ApiOperation("批量删除")
    @AutoLog("批量删除优惠券")
    public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        emsCouponService.deleteBatchEntityByDelFlag(ids);
        return Result.ok("批量删除成功");
    }


    @GetMapping(value = "/ems/emsCoupon/queryById")
    @ApiOperation("通过id查询")
    @AutoLog(value = "优惠券通过id查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryById(@RequestParam(name = "id", required = true) String id) {
        EmsCoupon emsCoupon = emsCouponService.getById(id);
        return Result.ok(emsCoupon);
    }

    @PostMapping(value = "/ems/emsCoupon/queryMemberCoupon")
    @ApiOperation("查询用户的优惠券")
    @AutoLog(value = "查询用户的优惠券", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryMemberCoupon(@RequestBody QueryMemberCouponRequest queryMemberCouponRequest){
        return Result.ok(emsCouponService.queryMemberCoupon(queryMemberCouponRequest));
    }

    @PostMapping(value = "/ems/emsCoupon/getCoupon")
    @ApiOperation("领取优惠券")
    @AutoLog("领取优惠券")
    public Result getCoupon(@RequestBody SaveMemberCouponRequest saveMemberCouponRequest) {
        emsCouponService.memberGetCoupon(saveMemberCouponRequest);
        return Result.ok("领取成功");
    }

    @DeleteMapping(value = "/ems/emsCoupon/deleteCoupon")
    @ApiOperation("用户删除优惠券")
    @AutoLog("用户删除优惠券")
    public Result deleteCoupon(@RequestBody DeleteMemberCouponRequest deleteMemberCouponRequest) {
        emsCouponService.deleteBatchEntityByDelFlag(deleteMemberCouponRequest.getTemplateId().toString());
        return Result.ok("删除优惠券成功");
    }

    @PostMapping(value = "/ems/emsCoupon/addMemberCoupon")
    @ApiOperation("用户注册赠送，默认优惠券")
    @AutoLog("用户注册赠送，默认优惠券")
    public Result addMemberCoupon(@RequestBody AddMemberCouponRequest addMemberCouponRequest) {
        emsCouponService.addMemberCoupon(addMemberCouponRequest);
        return Result.ok("领取成功");
    }

}
